/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.store.CashStoreItem;
import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.DrinksBrand;
import sg.edu.nus.iss.vmcs.store.DrinksStoreItem;
import sg.edu.nus.iss.vmcs.store.PropertyLoaderImpl;
import sg.edu.nus.iss.vmcs.store.StoreItem;
import java.io.File;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/**
 *
 * @author LinnLinn
 */
public class XmlPropertyLoaderImpl extends PropertyLoaderImpl{
    
    private String fileName;
    
    private static final String NAME_LABEL = "Name";
    private static final String WEIGHT_LABEL = "Weight";
    private static final String VALUE_LABEL = "Value";
    private static final String QUANTITY_LABEL = "Quantity";
    
    //private static final String NAME_LABEL     = "Name";
    private static final String PRICE_LABEL = "Price";
    //private static final String QUANTITY_LABEL = "Quantity";
    
    private Map<String, String> Items = new HashMap<String, String>();
    int total;

    public XmlPropertyLoaderImpl(String fileName) {
        this.fileName = fileName;
    }

    public void initialize() {
        try {
            File xmlFileName = new File(fileName);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFileName);
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Item");

            total = nList.getLength();

            int itemnum = 0;
            if (total == 5) {
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        itemnum = temp + 1;
                        Element eElement = (Element) nNode;

                        String item, itemval;
                        item = new String("Name" + itemnum);
                        itemval = getTagValue("Name", eElement);
                        System.out.println("sdsf" + getTagValue("Name", eElement));

                        Items.put(item, (String) itemval);

                        item = new String("Weight" + itemnum);
                        itemval = String.valueOf(Double.parseDouble(getTagValue("Weight", eElement)));
                        Items.put(item, itemval);

                        item = new String("Value" + itemnum);
                        Items.put(item, String.valueOf(Integer.parseInt(getTagValue("Value", eElement))));

                        item = new String("Quantity" + itemnum);
                        Items.put(item, String.valueOf(getTagValue("Quantity", eElement)));

                    }
                }
            } else {
                System.out.println("2");
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        itemnum = temp + 1;
                        Element eElement = (Element) nNode;
                        String item;
                        item = new String("Name" + itemnum);
                        Items.put(item, String.valueOf(getTagValue("Name", eElement)));

                        item = new String("Price" + itemnum);
                        Items.put(item, String.valueOf(getTagValue("Price", eElement)));

                        item = new String("Quantity" + itemnum);
                        Items.put(item, String.valueOf(getTagValue("Quantity", eElement)));
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("Exception" + e.getMessage());
        }
    }

    public void saveProperty() throws Exception {
        //FileOutputStream stream = new FileOutputStream(fileName);
        //prop.store(stream, "");
        //stream.close();
    }

    public int getNumOfItems() {
        //String nm = prop.getProperty(PROP_NUM_ITEMS);
        //int nmi;
        //nmi = Integer.parseInt(nm);
        //return nmi;
        return total;
    }

    public void setNumOfItems(int vl) {
        //prop.setProperty(PROP_NUM_ITEMS, String.valueOf(vl));
    }

    public StoreItem getItem(int index) {

        if (this.fileName.equals(Environment.getCashPropXml())) {

            int idx = index + 1;
            Coin coin = new Coin();

            String name = new String(NAME_LABEL + idx);
            String value = getValue(name);
            coin.setName(value);

            name = new String(WEIGHT_LABEL + idx);
            value = getValue(name);
            coin.setWeight(Double.parseDouble(value));

            name = new String(VALUE_LABEL + idx);
            value = getValue(name);
            coin.setValue(Integer.parseInt(value));

            name = new String(QUANTITY_LABEL + idx);
            value = getValue(name);
            int qty = Integer.parseInt(value);

            CashStoreItem item = new CashStoreItem(coin, qty);
            return item;
        } else if (this.fileName.equals(Environment.getDrinkPropXml())) {
            int idx = index + 1;
            DrinksBrand brand = new DrinksBrand();

            String name = new String(NAME_LABEL + idx);
            String value = getValue(name);
            brand.setName(value);

            name = new String(PRICE_LABEL + idx);
            value = getValue(name);
            brand.setPrice(Integer.parseInt(value));

            name = new String(QUANTITY_LABEL + idx);
            value = getValue(name);
            int qty = Integer.parseInt(value);

            DrinksStoreItem item = new DrinksStoreItem(brand, qty);
            return item;
        } else {
            return null;
        }
    }

    public void setItem(int index, StoreItem sitem) {
        if (this.fileName.equals(Environment.CASH_PROP_FILE)) {
            int idx = index + 1;

            CashStoreItem item = (CashStoreItem) sitem;
            Coin cn = (Coin) item.getContent();
            String itn = new String(NAME_LABEL + idx);

            setValue(itn, cn.getName());

            itn = new String(WEIGHT_LABEL + idx);
            setValue(itn, String.valueOf(cn.getWeight()));

            itn = new String(VALUE_LABEL + idx);
            setValue(itn, String.valueOf(cn.getValue()));

            itn = new String(QUANTITY_LABEL + idx);
            setValue(itn, String.valueOf(item.getQuantity()));
        } else {
            int idx = index + 1;

            DrinksStoreItem dsItem = (DrinksStoreItem) sitem;
            DrinksBrand brand = (DrinksBrand) dsItem.getContent();
            String itn = new String(NAME_LABEL + idx);
            setValue(itn, brand.getName());

            itn = new String(PRICE_LABEL + idx);
            setValue(itn, String.valueOf(brand.getPrice()));

            itn = new String(QUANTITY_LABEL + idx);
            setValue(itn, String.valueOf(dsItem.getQuantity()));
        }

    }

    public String getValue(String key) {
        //return prop.getProperty(key);
        return Items.get(key);
    }

    public void setValue(String key, String value) {
        //prop.setProperty(key, value);
        Items.put(key, value);
    }

    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
}
